package com.qing.module;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.qing.common.base.BaseActivity;
import com.qing.common.util.LogUtil;
import com.qing.common.view.DeviceButton;
import com.qing.module.databinding.MusicActivityBinding;
import com.qing.simple_player.player.IPlayer;
import com.qing.simple_player.viewmodel.AbsPlayerViewModel;
import com.qing.simple_player.viewmodel.SinglePlayerViewModel;

import java.io.File;

/**
 * @author qing <br/>
 * 2025/3/8
 */
public class MusicActivity extends BaseActivity<MusicActivityBinding> {

    AbsPlayerViewModel mViewModel;

    private int mCurrentState = IPlayer.PLAY_STATE_IDLE;

    @Override
    protected MusicActivityBinding createViewBinding() {
        return MusicActivityBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {
            @NonNull
            @Override
            @SuppressWarnings("unchecked")
            public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
                Intent sessionIntent = new Intent(MusicActivity.this, MusicActivity.class);
                return (T) new SinglePlayerViewModel(MusicActivity.this, "_music", sessionIntent);
            }
        }).get(AbsPlayerViewModel.class);

        mViewModel.getPlaybackState().observe(this, playbackState -> {
            LogUtil.INSTANCE.d(TAG, "播放状态：" + IPlayer.playStateToString(playbackState));
            mCurrentState = playbackState;
            switch (playbackState) {
                case IPlayer.PLAY_STATE_IDLE:
                    break;
                case IPlayer.PLAY_STATE_BUFFERING:
                    break;
                case IPlayer.PLAY_STATE_PLAYING:
                    getBinding().btnMusic.setType(DeviceButton.TYPE_PHONE_READING);
                    break;
                case IPlayer.PLAY_STATE_READY:
                    break;
                case IPlayer.PLAY_STATE_PAUSED:
                    getBinding().btnMusic.setType(DeviceButton.TYPE_PHONE);
                    break;
                case IPlayer.PLAY_STATE_SEEKING:
                    break;
                case IPlayer.PLAY_STATE_ERROR:
                    break;
                case IPlayer.PLAY_STATE_ENDED:
                    mViewModel.pause();
                    mViewModel.seekTo(0);
                    getBinding().btnMusic.setType(DeviceButton.TYPE_PHONE);
                    break;
            }
        });
        mViewModel.getPlayProgress().observe(this, progress -> {
            LogUtil.INSTANCE.d(TAG, "播放进度：" + progress);
        });
        mViewModel.getPlayDuration().observe(this, duration -> {
            LogUtil.INSTANCE.d(TAG, "播放时长：" + duration);
        });
        getBinding().btnMusic.setType(DeviceButton.TYPE_PHONE);
        // mViewModel.setMediaUrl("http://music.163.com/song/media/outer/url?id=137977.mp3");
        File dir = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        if (dir != null) {
            mViewModel.setMediaUrl(dir.getAbsolutePath() + File.separator + "music" + File.separator + "test.mp3");
        }
    }

    @Override
    protected void initListener() {
        getBinding().btnMusic.setOnClickListener(v -> {
            if (mCurrentState == IPlayer.PLAY_STATE_PLAYING) {
                mViewModel.pause();
            } else {
                mViewModel.play();
            }
        });
    }
}
